package com.designpattern.structure.proxy.jdkproxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @ClassName ProxyFactory
 * @Description 获取代理对象的工厂类
 * @Version 1.0
 **/
public class ProxyFactory {
    //声明代理对象
    private TrainStation trainStation = new TrainStation();

    public SellTickets getProxyInstance(){
        SellTickets proxy = (SellTickets) Proxy.newProxyInstance(
                trainStation.getClass().getClassLoader(),
                trainStation.getClass().getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        System.out.println("收取一些手续费");
                        Object invoke = method.invoke(trainStation, args);
                        return invoke;
                    }
                }
        );
        return proxy;
    }
}
